class Node(object):
    def __init__(self, data=0):
        self.data = data
        self.next = None


class List(object):
    def __init__(self):
        self.head = Node()
        self.length = 0

    def insert(self, pos, data):
        assert 0 <= pos <= self.length

        # Find put the position to insert.
        tmpPtr = self.head
        while pos > 0:
            tmpPtr = tmpPtr.next
            pos -= 1

        # insert new node
        newNode = Node()
        newNode.data = data
        newNode.next = tmpPtr.next
        tmpPtr.next = newNode
        self.length += 1

    def append(self, data):
        self.insert(self.length, data)

    def remove(self, pos):
        assert pos < self.length

        tmpPtr = self.head
        while pos > 0:
            tmpPtr = tmpPtr.next
            pos -= 1

        if tmpPtr.next is not None:
            delNode = tmpPtr.next
            tmpPtr.next = delNode.next
            self.length -= 1

    def foreach(self):
        tmpPtr = self.head.next
        while tmpPtr is not None:
            yield tmpPtr.data
            # 迭代器
            tmpPtr = tmpPtr.next


if __name__ == '__main__':
    l = List()
    l.append(1)
    l.append(0)
    l.append(9)
    print(list(l.foreach()))
